<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <div class="seach-box">
        <div class="select-box-content">
          <div class="select-box-content-name">关键字名称：</div>
          <el-input
            v-model="hitchKeywordName"
            clearable
            placeholder="请输入关键字名称"
            @change="queryPageList()"
          />
        </div>
        <div class="operationBtn">
          <div class="screenBtn">
            <div class="btn" @click="changeSelect()">
              <i
                :class="activeName === '' ? 'el-icon-d-arrow-right' : 'el-icon-d-arrow-left'"
                class="arrow-down"
              ></i>
            </div>
          </div>
        </div>
        <el-collapse v-model="activeName" accordion @change="handleChange">
          <el-collapse-item name="1">
            <template slot="title"></template>
            <div class="select-box-content">
              <div class="select-box-content-name">关联设备型号：</div>
              <div
                class="select-box-content-all"
                @click="cancelSelect('equModelCode')"
              >全部</div>
              <div
                v-for="(item, index) in equModelArr"
                :key="index"
                class="select-box-content-list"
                :style="{ color: equModelCode === item.equModelCode ? '#54DEE2' : '#C1CFD2' ,fontWeight : equModelCode === item.equModelCode ? '600':'400' }"
                @click="selectEquModelCode(item.equModelCode)"
              >{{ item.modelName }}</div>
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>
    </template>
    <template v-slot:btnsBar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :page-no="164"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:content>
      <e-table
        ref="tableData"
        rowKey="keywordCode"
        :table-data="tableData"
        :table-columns="tableLabel"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-option="tableOption"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @handleSelectionChange="handleSelectionChange"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template
          slot="operate"
          v-if="scoped.row.keywordCode"
          slot-scope="scoped"
        >
          <span
            v-if="judgePermission(164,'edit')"
            class="table-btns-box"
            @click="edit(scoped.row)"
          >
            <i class="el-icon-edit-outline"></i>
            <span>修改</span>
          </span>

          <span
            v-if="judgePermission(164,'deleteone')"
            class="table-btns-box table_del_btn"
            @click="delOne(scoped.row)"
          >
            <i class="el-icon-delete table_del_btn"></i>
            <span>删除</span>
          </span>
        </template>
      </e-table>

      <AddEditDialog
        ref="addEditDialog"
        :equ-model-arr="equModelArr"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import faultKeyManageApi from "@/api/equip-repair-support/fault-key-manage";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
// import equipTypeApi from "@/api/equip-manage/equip-type/index";
import equipModelApi from "@/api/equip-manage/equip-model";
import { mapGetters } from "vuex";
export default {
  name: "FaultKeyManage",
  components: { ActionBar, BaseMainSlot, AddEditDialog },
  mixins: [mymixin],
  data() {
    return {
      // 所搜条件：关键字名称
      hitchKeywordName: "",
      //  搜索条件:查询条件
      equModelCode: "",
      // 设备型号数组
      equModelArr: [],
      checkedIdList: [],
      tableLabel: [
        {
          label: "关键字名称",
          param: "hitchKeywordName",
          show: true,
        },
        {
          label: "关联设备型号",
          param: "equModelName",
          show: true,
          selectValue: "equModelCode",
          type: "Select",
          typeObj: {
            listName: ["modelName", "equModelCode"],
          },
        },
        {
          label: "备注",
          param: "description",
          show: true,
          notSearch: true,
        },
      ],
      // 表单事件
      tableOption: {
        label: "操作",
        options: [
          {
            label: "操作",
            slotName: "operate",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {
    tableLabel: {
      handler(v) {
        console.log("tableLabel watch", v);
      },
      immediate: true,
    },
  },
  created() {
    this.queryPageList();
    this.queryEquipModel();
  },
  mounted() {},
  methods: {
    // 清空选项
    cancelSelect(data) {
      if (data === "equModelCode") {
        this.equModelCode = "";
      }
      this.queryPageList();
    },
    // 选择
    selectEquModelCode(id) {
      if (this.equModelCode === id) {
        this.equModelCode = "";
      } else {
        this.equModelCode = id;
      }
      this.queryPageList();
    },
    // 设备型号
    queryEquipModel() {
      this.loading = false;
      let params = {
        page: 1,
        size: 1000,
      };
      // 获取数据
      equipModelApi.queryPageList(params).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data || {};

          this.equModelArr = data.content || [];
          console.log("queryEquipModel start", this.tableLabel);
          this.tableLabel.forEach((item) => {
            if (item.param === "equModelName") {
              console.log("queryEquipModel item", item);
              const new_data = this.equModelArr.map((item2) => {
                return {
                  modelName: item2.modelName,
                  equModelCode: item2.equModelCode,
                };
              });
              this.$set(item.typeObj, "list", new_data);
              return;
            }
          });
          console.log("queryEquipModel new", this.tableLabel);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    queryPageList(e) {
      this.loading = false;
      // 获取数据
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          equModelCode: this.equModelCode || void 0,
          hitchKeywordName: this.hitchKeywordName || void 0,
          ...e,
        },
      };
      faultKeyManageApi.queryPageList(param).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },

    selectAll(list) {
      let notvalue = true;
      list.forEach((item) => {
        if (!item.keywordCode) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select(list) {
      let notvalue = true;
      list.forEach((item) => {
        if (!item.keywordCode) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map((item) => {
        if (item.keywordCode) {
          return item.keywordCode;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          var form = new FormData();
          form.append("hitchKeywordCodeList", data.keywordCode);
          faultKeyManageApi.delList(form).then((res) => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.queryPageList();
              this.resetActionBar();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 表格删除
    delmessage() {
      var form = new FormData();
      this.checkedIdList.forEach((a) => {
        form.append("hitchKeywordCodeList", a);
      });
      faultKeyManageApi.delList(form).then((res) => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.keywordCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-collapse-item__header {
  height: 0px;
}
::v-deep .el-collapse-item__content {
  padding-bottom: 10px;
}
</style>
